import pygame
import sys

# 初始化Pygame
pygame.init()

# 设置窗口大小
screen_width = 800
screen_height = 600
screen = pygame.display.set_mode((screen_width, screen_height))

# 设置标题
pygame.display.set_caption("简单的小球游戏")

# 颜色定义
white = (255, 255, 255)
red = (255, 0, 0)

# 小球的位置和速度
ball_pos = [100, 100]
ball_speed = [2, 2]
ball_radius = 10

# 游戏主循环
running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    # 更新小球位置
    ball_pos[0] += ball_speed[0]
    ball_pos[1] += ball_speed[1]

    # 检查小球是否碰撞到窗口边界
    if ball_pos[0] < 0 or ball_pos[0] > screen_width - ball_radius * 2:
        ball_speed[0] = -ball_speed[0]
    if ball_pos[1] < 0 or ball_pos[1] > screen_height - ball_radius * 2:
        ball_speed[1] = -ball_speed[1]

    # 填充背景颜色
    screen.fill(white)

    # 绘制小球
    pygame.draw.circle(screen, red, ball_pos, ball_radius)

    # 更新屏幕显示
    pygame.display.flip()

    # 控制游戏帧率
    pygame.time.delay(10)

# 清理并退出Pygame
pygame.quit()
sys.exit()